package com.coderush.admin.sys.controller;

import com.coderush.admin.core.common.PageResult;
import com.coderush.admin.core.response.Result;
import com.coderush.admin.sys.model.dto.RoleAddDto;
import com.coderush.admin.sys.model.dto.RoleUpdateDto;
import com.coderush.admin.sys.model.vo.RolePageVo;
import com.coderush.admin.sys.service.RoleService;
import io.swagger.v3.oas.annotations.Operation;
import io.swagger.v3.oas.annotations.responses.ApiResponse;
import io.swagger.v3.oas.annotations.responses.ApiResponses;
import io.swagger.v3.oas.annotations.tags.Tag;
import jakarta.annotation.Resource;
import org.springframework.validation.annotation.Validated;
import org.springframework.web.bind.annotation.*;

/**
 * PackageName com.coderush.admin.sys.controller
 *
 * @Author Eason
 * @Version v1.0
 * @ClassName RoleController
 * @Date 2025/8/29
 * @Description TODO
 */
@RestController
@RequestMapping("/sys/role")
@Validated  // 类级别添加，用于方法参数校验
@Tag(name = "角色管理", description = "角色管理")
public class RoleController {

    @Resource
    private RoleService roleService;

    /**
     * 分页查询角色列表
     */
    @Operation(summary = "分页查询角色列表", description = "分页查询角色列表")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "401", description = "未授权"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @GetMapping("/findRolePage")
    public Result<PageResult<RolePageVo>> findRolePage(String roleName, Integer pageNumber, Integer pageSize) {
        PageResult<RolePageVo> pageResult = roleService.findRolePage(pageNumber, pageSize, roleName);
        return Result.buildSuccess(pageResult);
    }

    /**
     * 删除角色
     */
    @Operation(summary = "删除角色", description = "删除角色")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "401", description = "未授权"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @DeleteMapping("/deleteById/{id}")
    public Result<Boolean> deleteById(@PathVariable Long id) throws Exception {
        return Result.buildSuccess(roleService.deleteById(id));
    }

    /**
     * 修改角色
     */
    @Operation(summary = "修改角色", description = "修改角色")
    @ApiResponses({
            @ApiResponse(responseCode = "200", description = "操作成功"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PutMapping("/update")
    public Result<Boolean> update(@Validated @RequestBody RoleUpdateDto updateDto) throws Exception {
        return Result.buildSuccess(roleService.update(updateDto));
    }

    /**
     * 添加角色
     */
    @Operation(summary = "添加角色", description = "添加角色")
    @ApiResponses(value = {
            @ApiResponse(responseCode = "200", description = "成功"),
            @ApiResponse(responseCode = "401", description = "未授权"),
            @ApiResponse(responseCode = "500", description = "服务器内部错误")
    })
    @PostMapping("/add")
    public Result<Boolean> add(@Validated @RequestBody RoleAddDto addDto) throws Exception {
        boolean flag = roleService.add(addDto);
        return Result.buildSuccess(flag);
    }
}